package com.example.blog.controller;

import com.example.blog.entity.Article;
import com.example.blog.entity.Comment;
import com.example.blog.entity.User;
import com.example.blog.entity.likes;
import com.example.blog.mapper.ArticleMapper;
import com.example.blog.mapper.CommentMapper;
import com.example.blog.mapper.UserMapper;
import com.example.blog.repository.LikesRepository;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@Slf4j
public class ArticleCtl {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private LikesRepository likesRepository;

    @RequestMapping("/articles/{id}")
    public String getArticleById(@PathVariable int id, Model model,HttpSession session) {
        Article article = articleMapper.selectArticleById(id);
        User user=(User)session.getAttribute("user");
        List<Comment> comments = commentMapper.selectCommentsByArticleId(id);
        likes like111=likesRepository.findByArticleIdAndUserId(id, user.getId());
        model.addAttribute("article", article);
        model.addAttribute("likes",like111);
        model.addAttribute("comments",comments);
        return "articles";
    }
    @PostMapping("/delete")
    public String deleteArticle(@RequestParam int id, RedirectAttributes redirectAttributes) {
        int result = articleMapper.deleteArticleById(id);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "文章删除成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "文章删除失败！");
        }
        return "redirect:/login/users";
    }

    // 获取文章用于修改，跳转到修改页面
    @GetMapping("/editArticle")
    public String getArticleForEdit(@RequestParam("id") Integer id, Model model) {
        Article article = articleMapper.selectArticleById(id);
        model.addAttribute("article", article);
        return "updateArticle";
    }

    // 处理文章更新操作
    @PostMapping("/updateArticle")
    public String updateArticle(@ModelAttribute("article") Article article,
                                RedirectAttributes redirectAttributes) {
        System.out.println(article.getId());
        System.out.println(article.getTitle());
        System.out.println(article.getContent());
        int result = articleMapper.updateArticle(article);
        System.out.println(result);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "文章更新成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "文章更新失败！");
        }
        return "redirect:/login/users";
    }

    @GetMapping("/allarticles")
    public String getAllArticles(Model model) {
        List<Article> articles = articleMapper.selectAllArticles();
        model.addAttribute("articles", articles);
        return "allarticles";
    }


    @GetMapping("/editUser")
    public String getUserForEdit(@RequestParam("id") Integer id, Model model) {
        System.out.println(id);
        User user = userMapper.selectUserById(id);
        System.out.println(user.getGender());
        model.addAttribute("user", user);
        return "updateUser";
    }

    @PostMapping("/updateUser")
    public String updateUser(@RequestParam("id") int id,
                             @RequestParam("name") String name,
                             @RequestParam("password") String password,
                             @RequestParam("age") int age,
                             @RequestParam("gender") String gender,
                             HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null && user.getId() == id) {
            user.setName(name);
            user.setAge(age);
            user.setGender(gender);
            user.setPassword(password);
            userMapper.updateUser(user);
            return "redirect:/login/users";
        } else {
            return "login";
        }
    }

}